package cn.hrlly.tao2shou.biz;

import android.content.Context;

import com.zhy.http.okhttp.OkHttpUtils;

import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.callback.CommoditiesListCallback;
import cn.hrlly.tao2shou.domain.CommodityList;
import cn.hrlly.tao2shou.listener.OnCommoditiesListListener;
import cn.hrlly.tao2shou.utils.CacheUtil;

/**
 * User: gengliang
 * Date: 2016-01-28
 * Time: 18:30
 */

public class CommoditiesListBizImpl implements CommoditiesListBiz {

    private OnCommoditiesListListener onCommoditiesListListener;
    private CacheUtil cacheUtil;

    public CommoditiesListBizImpl(OnCommoditiesListListener onCommoditiesListListener,Context context){
        this.onCommoditiesListListener = onCommoditiesListListener;
        this.cacheUtil=new CacheUtil(context);
    }

    @Override
    public void showCommoditiesList(CommodityList commodityList) {
        OkHttpUtils
                .post()
                .url(Config.key_url)
                .addParams(Config.key_action,Config.action_commodities_list)
                .addParams(Config.key_page, String.valueOf(commodityList.getPage()))
                .addParams(Config.key_page_count, String.valueOf(commodityList.getPageCount()))
                .addParams(Config.key_sort_according, String.valueOf(commodityList.getSortAccording()))
                .addParams(Config.key_longditude, String.valueOf(commodityList.getLongitude()))
                .addParams(Config.key_lantitude, String.valueOf(commodityList.getLantitude()))
                .build()
                .execute(new CommoditiesListCallback(onCommoditiesListListener));
    }

    @Override
    public void saveCacheCommoditiesList(CommodityList commodityList) {
        cacheUtil.putCommodityList(commodityList);
    }

    @Override
    public CommodityList getCacheCommoditiesList() {
        CommodityList commodityList=cacheUtil.getCommodityList();
        return commodityList;
    }
}
